package org.zjt.hdfs.demo;

import java.io.*;
import java.net.URI;

import org.apache.hadoop.fs.*;
import org.apache.hadoop.conf.*;

/**
 * 设置fs.defaultFS参数，如果没有设置，会出现java.lang.IllegalArgumentException:
 * Wrong FS:hdfs://master:9000/user,expected: file:///
 * 当然了，也可以将hadoop集群中的core-site.xml配置文件拷贝到该工程项目下,这样，在读取配置文件时就能够识别hdfs文件系统
 */
public class HDFSRead {
    public static void main(String[] args) throws Exception {
        try {
            Path dest = new Path("hdfs://centos:9000/user/apollo-configservice.log");

            Configuration conf = new Configuration();// 读取配置文件
            /*
                todo: 可无
                conf.set("fs.defaultFS", "hdfs://master:9000/");
                conf.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
            */
            FileSystem fs = FileSystem.get(new URI("hdfs://centos:9000/"), conf, "root");// 获取hdfs实例

            BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(dest)));
            String line;
            line = br.readLine();
            while (line != null) {
                System.out.println(line);
                line = br.readLine();
            }
            fs.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("File not found");
        }
    }
}
